#include"queue.h"
void Init(queue* pq)
{
    assert(pq);
    pq->head = NULL;
    pq->tail = NULL;
}
void Destory(queue* pq)
{
    assert(pq);
    free(pq->head);
    free(pq->tail);
    pq->head = pq->tail = NULL;
}
void push(queue* pq,DataType x)
{
    assert(pq);
    Node* newnode = (Node*)malloc(sizeof(Node));
    newnode->next = NULL;
    newnode->val = x;
    if(pq->head == NULL) //当前队列为空
    {
        pq->head = pq->tail = newnode;
    }
    else
    {
        pq->tail->next  = newnode;
        pq->tail = newnode;//换尾
    }
}
void pop(queue* pq)
{
    assert(pq && pq->head); //防止队列为空
    Node* tmp = pq->head;
    pq->head  = pq->head->next;//换头
    free(tmp);
    //注意:有可能删没了->此时pq->tail不置为空就会造成野指针
    if(pq->head == NULL)
        pq->tail = NULL;
}
DataType front(queue* pq)
{
    assert(pq && pq->head);
    return pq->head->val;
}
DataType back(queue* pq)
{
    assert(pq && pq->head);
    return pq->tail->val;
}
size_t size(queue* pq)
{
    size_t count = 0;
    Node* cur = pq->head;
    while(cur)
    {
        count++;
        cur = cur->next;
    }
    return count;
}
bool empty(queue* pq)
{
    assert(pq);
    return pq->head == NULL;
}
